package controllers

import dao._
import implicits.Implicits._
import play.api.cache.AsyncCacheApi
import play.api.mvc._
import tool.FormTool
import ujson.play.PlayJson
import upickle.default._

import javax.inject.Inject
import scala.concurrent.ExecutionContext.Implicits.global

/**
 * Created by yz on 16/9/2021
 */
class ExpH2Controller @Inject()(cc: ControllerComponents, cache: AsyncCacheApi)(
  implicit val basicDao: ExpH2Dao,
) extends AbstractController(cc) {

  def getByGeneId = Action.async { implicit request =>
    val data = FormTool.geneIdForm.bindFromRequest().get
    basicDao.selectOp(data.geneId).map { x =>
      Ok(writeJs(x))
    }
  }

  def allByServer = Action.async { implicit request =>
    val obj = PlayJson.transform(request.body.asJson.get, reader[ujson.Obj])
    val pageData = FormTool.pageForm.bindFromRequest().get
    basicDao.selectAll(obj.value, pageData).map { case (size, x) =>
      val array = x.map { y =>
        ujson.Obj("geneId" -> y.geneId) ++ y.value.obj
      }
      Ok(ujson.Obj("rows" -> array, "total" -> size))
    }
  }


}
